/**
 * 
 */
package com.nervytech.tutorcruise.spring.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nervytech.tutorcruise.db.service.interfaces.ICompanyProfileService;
import com.nervytech.tutorcruise.db.service.interfaces.IInstituteProfileService;
import com.nervytech.tutorcruise.db.service.interfaces.ITrainerProfileService;
import com.nervytech.tutorcruise.db.service.interfaces.IUserService;

/**
 * @author ADMIN
 * 
 */
@Controller
@RequestMapping(value = "/usr/ajax/profile")
public class ProfileAjaxController {
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IInstituteProfileService instituteProfileService;
	
	@Autowired
	private ICompanyProfileService companyProfileService;
	
	@Autowired
	private ITrainerProfileService trainerProfileService;
	
	/*
	 // TO pass a huge data to datbase in POST ...
	var encodedHTML = escape(Html);         //here encoding Html.
	$.ajax({
	                type: "POST",
	                url: "Post.aspx/savePost",
	                data: "{Title:'" + Title + "',Html:'" + encodedHTML + "'}",
	                contentType: "application/json",
	                dataType: "json",
	                success: function (data) {
	                    if (data == undefined) {
	                        alert("Error : 219");
	                    }
	                    else {
	                        alert(data.d);
	                    }
	                },
	                error: function (data) {
	                    if (data == undefined) {
	                        alert("Error : 465");
	                    }
	                    else {
	                        alert("Error : 468 " + data.d);
	                    }
	                }
	            });*/

	@RequestMapping(value = "/update/trainer/{userId}", method = RequestMethod.POST)
	public @ResponseBody
	String updateTrainer(@PathVariable String userId, Map model, HttpServletRequest request) {
		
		String message = "Success";
				
		String updateType = request.getParameter("type");
		
		// TODO : either change do switch case by having some id or have separate methods ...
		if(updateType.equals("techingexp")){
			trainerProfileService.updateTeachingExperience(request.getParameter("techingexpstr"),userId);
		} else if(updateType.equals("profexp")) {
			trainerProfileService.updateProfExperience(request.getParameter("profexpstr"),userId);
		} else if(updateType.equals("skillsum")) {
			trainerProfileService.updateSkillSummary(request.getParameter("skillsumstr"),userId);
		} else if(updateType.equals("edusum")) {
			trainerProfileService.updateEduSummary(request.getParameter("edusumstr"),userId);
		} else if(updateType.equals("certsum")) {
			// TODO : Also set is_certified to true
			trainerProfileService.updateCertSummary(request.getParameter("certsumstr"),userId);
		} else if(updateType.equals("taggedSkills")) {
			trainerProfileService.updateTaggedSkills(request.getParameter("taggedSkillsStr"),userId);
		} else if(updateType.equals("teachandprofyears")) {
			trainerProfileService.updateExpYears(request.getParameter("teachyears"),request.getParameter("profyears"),userId);
		} else if(updateType.equals("socials")) {
			trainerProfileService.updateSocials(request.getParameter("lnurl"),request.getParameter("fburl"),request.getParameter("twurl"),request.getParameter("gpurl"),userId);
		} else if(updateType.equals("blogs")) {
			trainerProfileService.updateBlogs(request.getParameter("blogs"),userId);
		} else if(updateType.equals("title")) {
			trainerProfileService.updateTitle(request.getParameter("title"),userId);
		} else if(updateType.equals("fullname")) {
			trainerProfileService.updateFullName(request.getParameter("fullname"),userId);
		} else if(updateType.equals("area")) {
			trainerProfileService.updateArea(request.getParameter("country"),request.getParameter("city"),request.getParameter("zipcode"),userId);
		} else if(updateType.equals("achievesum")) {
			trainerProfileService.updateAchievementSummary(request.getParameter("achievesum"),userId);
		}
		
		return message;

	}
	
	@RequestMapping(value = "/update/trainee/{userId}", method = RequestMethod.POST)
	public @ResponseBody
	String updateTrainee(@PathVariable String userId, Map model, HttpServletRequest request) {
		
		String message = "Success";
				
		String updateType = request.getParameter("type");
		
		// TODO : either change do switch case by having some id or have separate methods ...
		if(updateType.equals("techingexp")){
			trainerProfileService.updateTeachingExperience(request.getParameter("techingexpstr"),userId);
		} else if(updateType.equals("profexp")) {
			trainerProfileService.updateProfExperience(request.getParameter("profexpstr"),userId);
		} else if(updateType.equals("skillsum")) {
			trainerProfileService.updateSkillSummary(request.getParameter("skillsumstr"),userId);
		} else if(updateType.equals("edusum")) {
			trainerProfileService.updateEduSummary(request.getParameter("edusumstr"),userId);
		} else if(updateType.equals("certsum")) {
			// TODO : Also set is_certified to true
			trainerProfileService.updateCertSummary(request.getParameter("certsumstr"),userId);
		} else if(updateType.equals("taggedSkills")) {
			trainerProfileService.updateTaggedSkills(request.getParameter("taggedSkillsStr"),userId);
		} else if(updateType.equals("teachandprofyears")) {
			trainerProfileService.updateExpYears(request.getParameter("teachyears"),request.getParameter("profyears"),userId);
		} else if(updateType.equals("socials")) {
			trainerProfileService.updateSocials(request.getParameter("lnurl"),request.getParameter("fburl"),request.getParameter("twurl"),request.getParameter("gpurl"),userId);
		} else if(updateType.equals("blogs")) {
			trainerProfileService.updateBlogs(request.getParameter("blogs"),userId);
		} else if(updateType.equals("title")) {
			trainerProfileService.updateTitle(request.getParameter("title"),userId);
		} else if(updateType.equals("fullname")) {
			trainerProfileService.updateFullName(request.getParameter("fullname"),userId);
		} else if(updateType.equals("area")) {
			trainerProfileService.updateArea(request.getParameter("country"),request.getParameter("city"),request.getParameter("zipcode"),userId);
		} else if(updateType.equals("achievesum")) {
			trainerProfileService.updateAchievementSummary(request.getParameter("achievesum"),userId);
		}
		
		return message;

	}
	
	@RequestMapping(value = "/update/institute/{userId}", method = RequestMethod.POST)
	public @ResponseBody
	String updateInstitute(@PathVariable String userId, Map model, HttpServletRequest request) {
		
		String message = "Success";
				
		String updateType = request.getParameter("type");
		
		// TODO : either change do switch case by having some id or have separate methods ...
		if(updateType.equals("techingexp")){
			trainerProfileService.updateTeachingExperience(request.getParameter("techingexpstr"),userId);
		} else if(updateType.equals("profexp")) {
			trainerProfileService.updateProfExperience(request.getParameter("profexpstr"),userId);
		} else if(updateType.equals("skillsum")) {
			trainerProfileService.updateSkillSummary(request.getParameter("skillsumstr"),userId);
		} else if(updateType.equals("edusum")) {
			trainerProfileService.updateEduSummary(request.getParameter("edusumstr"),userId);
		} else if(updateType.equals("certsum")) {
			// TODO : Also set is_certified to true
			trainerProfileService.updateCertSummary(request.getParameter("certsumstr"),userId);
		} else if(updateType.equals("taggedSkills")) {
			trainerProfileService.updateTaggedSkills(request.getParameter("taggedSkillsStr"),userId);
		} else if(updateType.equals("teachandprofyears")) {
			trainerProfileService.updateExpYears(request.getParameter("teachyears"),request.getParameter("profyears"),userId);
		} else if(updateType.equals("socials")) {
			trainerProfileService.updateSocials(request.getParameter("lnurl"),request.getParameter("fburl"),request.getParameter("twurl"),request.getParameter("gpurl"),userId);
		} else if(updateType.equals("blogs")) {
			trainerProfileService.updateBlogs(request.getParameter("blogs"),userId);
		} else if(updateType.equals("title")) {
			trainerProfileService.updateTitle(request.getParameter("title"),userId);
		} else if(updateType.equals("fullname")) {
			trainerProfileService.updateFullName(request.getParameter("fullname"),userId);
		} else if(updateType.equals("area")) {
			trainerProfileService.updateArea(request.getParameter("country"),request.getParameter("city"),request.getParameter("zipcode"),userId);
		} else if(updateType.equals("achievesum")) {
			trainerProfileService.updateAchievementSummary(request.getParameter("achievesum"),userId);
		}
		
		return message;

	}
	
	@RequestMapping(value = "/update/company/{userId}", method = RequestMethod.POST)
	public @ResponseBody
	String updateCompany(@PathVariable String userId, Map model, HttpServletRequest request) {
		
		String message = "Success";
				
		String updateType = request.getParameter("type");
		
		// TODO : either change do switch case by having some id or have separate methods ...
		if(updateType.equals("techingexp")){
			trainerProfileService.updateTeachingExperience(request.getParameter("techingexpstr"),userId);
		} else if(updateType.equals("profexp")) {
			trainerProfileService.updateProfExperience(request.getParameter("profexpstr"),userId);
		} else if(updateType.equals("skillsum")) {
			trainerProfileService.updateSkillSummary(request.getParameter("skillsumstr"),userId);
		} else if(updateType.equals("edusum")) {
			trainerProfileService.updateEduSummary(request.getParameter("edusumstr"),userId);
		} else if(updateType.equals("certsum")) {
			// TODO : Also set is_certified to true
			trainerProfileService.updateCertSummary(request.getParameter("certsumstr"),userId);
		} else if(updateType.equals("taggedSkills")) {
			trainerProfileService.updateTaggedSkills(request.getParameter("taggedSkillsStr"),userId);
		} else if(updateType.equals("teachandprofyears")) {
			trainerProfileService.updateExpYears(request.getParameter("teachyears"),request.getParameter("profyears"),userId);
		} else if(updateType.equals("socials")) {
			trainerProfileService.updateSocials(request.getParameter("lnurl"),request.getParameter("fburl"),request.getParameter("twurl"),request.getParameter("gpurl"),userId);
		} else if(updateType.equals("blogs")) {
			trainerProfileService.updateBlogs(request.getParameter("blogs"),userId);
		} else if(updateType.equals("title")) {
			trainerProfileService.updateTitle(request.getParameter("title"),userId);
		} else if(updateType.equals("fullname")) {
			trainerProfileService.updateFullName(request.getParameter("fullname"),userId);
		} else if(updateType.equals("area")) {
			trainerProfileService.updateArea(request.getParameter("country"),request.getParameter("city"),request.getParameter("zipcode"),userId);
		} else if(updateType.equals("achievesum")) {
			trainerProfileService.updateAchievementSummary(request.getParameter("achievesum"),userId);
		}
		
		return message;

	}
}
